package com.miyu.rabbitmq.config;



import com.rabbitmq.client.Channel;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class MqUtils {
    /**
     * 获取工厂配置类
     */
    @Resource
    private ConnectionFactory connectionFactory;

    /**
     * 新增消息对列
     */
    public void mqOperate(MqVo mqVo) {

        //交换机类型
        String exchangeType = mqVo.getExchangeType();
        log.info("exchangeType -> {}", exchangeType);

        //队列名称
        String queueName = mqVo.getQueueName();
        log.info("queueName -> {}", queueName);

        //交换机名称
        String exchangeName = mqVo.getExchangeName();
        log.info("exchangeName -> {}", exchangeName);

        //绑定关系
        String binding = mqVo.getBinding();
        log.info("binding -> {}", binding);

        //是否死信队列(0:是;1:否)
        String delayType = mqVo.getDelayType();
        log.info("delayType -> {}", delayType);

        //操作类型
        int status = mqVo.getType();

        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        if (status == 0) {
            //新增队列
            rabbitAdmin.declareQueue(new Queue(queueName));

            //新增交换机
            rabbitAdmin.declareExchange(getExchange(exchangeType, exchangeName, delayType));

            //新增绑定关系
            rabbitAdmin.declareBinding(new Binding(queueName, Binding.DestinationType.QUEUE, exchangeName, binding, null));
        } else {
            //删除队列
            rabbitAdmin.deleteQueue(queueName);
            //删除交换机
            rabbitAdmin.deleteExchange(exchangeName);
            //删除绑定关系
            rabbitAdmin.removeBinding(new Binding(queueName, Binding.DestinationType.QUEUE, exchangeName, binding, null));
        }
        close();
    }

    /**
     * 交换机生成方法
     * @param exchangeType 交换机类型
     * @param exchangeName 交换机名称
     * @param delayType 是否死信队列(0:是;1:否)
     * @return Exchange
     */
    private Exchange getExchange(String exchangeType, String exchangeName, String delayType) {
        Exchange exchange = new DirectExchange(exchangeName);
        String zero = "0";
        switch (exchangeType) {
            case "0":
                if (zero.equals(delayType)) {
                    Map<String, Object> map = new HashMap<>(1);
                    map.put("x-delayed-type", "direct");
                    exchange = new CustomExchange(exchangeName, "x-delayed-message", true, false, map);
                } else {
                    exchange = new DirectExchange(exchangeName);
                }
                break;
            case "1":
                if (zero.equals(delayType)) {
                    //待补充
                } else {
                    exchange = new TopicExchange(exchangeName);
                }
                break;
            case "2":
                if (zero.equals(delayType)) {
                    //待补充
                } else {
                    exchange = new FanoutExchange(exchangeName);
                }
                break;
            case "3":
                if (zero.equals(delayType)) {
                    //待补充
                } else {
                    exchange = new HeadersExchange(exchangeName);
                }
                break;
            default:
                break;
        }
        return exchange;
    }

    /**
     * 关闭连接
     */
    public void close(){

        try (Connection connection = connectionFactory.createConnection()){
            try (Channel channel = connection.createChannel(true)){
                com.rabbitmq.client.Connection connection1 = channel.getConnection();
                connection1.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
